package com.zrc.module_find.activity.defrecycleview.demo1

import android.annotation.SuppressLint
import com.alibaba.android.arouter.facade.annotation.Route
import com.zrc.superframe_base.ARouterHub
import com.zrc.superframe_base.mvx.mvvm.BaseMvvmFragment
import com.zrc.module_find.R
import com.zrc.module_find.BR
import com.zrc.module_find.databinding.FragmentDemo1Binding
import com.zrc.module_find.viewmodel.FindVM
import kotlinx.android.synthetic.main.item_layout.*

/**
 * @Author: superman
 * @CreateTime: 6/27/21
 * @Describe:
 */
@Route(path = ARouterHub.FRAGMENT_DEMO1, name = "不一样过")
class Demo1Fragment : BaseMvvmFragment<FragmentDemo1Binding, FindVM>() {

    private lateinit var mDataModels: ArrayList<CoinInfo>
    private lateinit var adapter: CoinAdapter

    override fun getVariableId() = BR.viewModel

    override fun getLayoutId() = R.layout.fragment_demo1

    override fun getViewModel() = FindVM(this)

    @SuppressLint("UseRequireInsteadOfGet")
    override fun initObserver() {
        super.initObserver()

        mDataModels = ArrayList()
        adapter = CoinAdapter(activity!!, mDataModels, R.layout.item_layout)
        mBinding.idHrecyclerview.setAdapter(adapter,ll_move)

        //下拉刷新
        mBinding.smartRefreshLayout.setOnRefreshListener {
            getData(true)
        }

        mBinding.smartRefreshLayout.setOnLoadMoreListener {
            getData(false)
        }

        getData(true)
    }


    @SuppressLint("NotifyDataSetChanged")
    private fun getData(isRefrensh: Boolean = true) {
        if (isRefrensh) {
            mDataModels.clear()
        }
        for (i in 0..20) {
            val coinInfo = CoinInfo()
            coinInfo.name = "USDT"
            coinInfo.priceLast = "20.0"
            coinInfo.riseRate24 = "0.2"
            coinInfo.vol24 = "10020"
            coinInfo.close = "22.2"
            coinInfo.open = "40.0"
            coinInfo.bid = "33.2"
            coinInfo.ask = "19.0"
            coinInfo.amountPercent = "33.3%"
            mDataModels.add(coinInfo)
        }
        adapter.notifyDataSetChanged()
        if (isRefrensh) {
            mBinding.smartRefreshLayout.finishRefresh(2000)
        } else {
            mBinding.smartRefreshLayout.finishLoadMore(2000)
        }
    }
}